package skiree.host.abetsy.api.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import skiree.host.abetsy.api.service.EpisodeService;
import skiree.host.abetsy.api.vessel.Result;
import skiree.host.abetsy.model.LocalEpisode;

import javax.annotation.Resource;

@Controller
@CrossOrigin
@RequestMapping("/api/private/v1")
public class EpisodeController {

    @Resource
    private EpisodeService episodeService;

    @GetMapping("/episode/list")
    @ResponseBody
    public Result episodeList(@RequestParam(required = false, defaultValue = "0") int animeId,
                              @RequestParam(required = false, defaultValue = "") String query,
                              @RequestParam(required = false, defaultValue = "1") int pageNum,
                              @RequestParam(required = false, defaultValue = "9") int pageSize) {
        return episodeService.list(animeId, query, pageNum, pageSize);
    }

    @GetMapping("/episode/shows")
    @ResponseBody
    public Result episodeShows(@RequestParam(required = false, defaultValue = "0") int animeId) {
        return episodeService.shows(animeId);
    }

    @PostMapping("/episode/add")
    @ResponseBody
    public Result episodeAdd(@RequestBody LocalEpisode localEpisode) {
        return episodeService.add(localEpisode);
    }

    @PostMapping("/episode/edit")
    @ResponseBody
    public Result episodeEdit(@RequestBody LocalEpisode localEpisode) {
        return episodeService.edit(localEpisode);
    }

    @GetMapping("/episode/delete/{id}")
    @ResponseBody
    public Result episodeDelete(@PathVariable("id") int id) {
        episodeService.delete(id);
        return Result.success(null);
    }

    @GetMapping("/episode/refresh/{id}")
    @ResponseBody
    public Result episodeRefresh(@PathVariable("id") int id) {
        episodeService.refresh(id);
        return Result.success(null);
    }

}
